<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/17
 * Time: 14:19
 */

namespace app\admin\controller;

use think\Db;
use think\Validate;


/**
 * Class Customer
 * @package app\admin\controller
 *  合作商家
 */
class Customer extends Base
{

    public function index()
    {
        $num = Db::name('Customer')->count();
        $pageObject = new \Page($num);
        $list = Db::name('Customer')->select();
        $map = array(
            'num' => $num,
            'list' => $list,
            'page' => $pageObject->showPages()
        );

        return view('index', $map);

    }

    public function add()
    {
        $rules = array(
            array('name', 'require|unique:Customer', '合作商家名字不能为空|合作商家名不能重复！'),
            array('title', 'require', '合作商家标题不能为空'),
        );
        $validate = new Validate($rules);
        $data = $_POST;
        $data['create_time'] = time();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }
        if (Db::name('Customer')->insertGetId($data)) {
            $this->success('添加成功！');
        } else {
            $this->error('添加失败！');
        }
    }

    public function edit()
    {
        if ($_POST) {
            $rules = array(
                array('name', 'require', '合作商家名字不能为空'),
                array('title', 'require', '合作商家标题不能为空'),
            );
            $validate = new Validate($rules);
            $data = $_POST;
            $data['create_time'] = time();
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
            if (Db::name('Customer')->update($data)) {
                $this->success('修改成功！');
            } else {
                $this->error('修改失败！');
            }
        } else {
            $id = input('id');
            $list = Db::name('Customer')->find(array('id'=>$id));
            $map = array(
                'list'=>$list
            );
            return view('edit',$map);
        }

    }

}